package com.wushuu.ce.ui.activity;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.wushuu.ce.R;
import com.wushuu.ce.model.Expression;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

/**
 * Created by js on 2016/5/25.
 */
@ContentView(R.layout.activity_practice)
public class PracticeActivity extends BaseActivity{

    private Context context;
    private List<Expression> mPracticeList;
    private Expression mExpression;
    private Integer[] mAnswerList;
    private PracticeAdapter adapter;

    @ViewInject(R.id.gv_practice_answer)
    private GridView gvPracticeAnswer;

    @ViewInject(R.id.tv_practice)
    private TextView tvPractice;

    @ViewInject(R.id.tv_practice_title)
    private TextView tvPracticeTitle;

    @Override
    public void init() {
        context = this;
        mPracticeList = (List<Expression>) getIntent().getSerializableExtra("PracticeList");
        mAnswerList = new Integer[4];
        showPractice(0);
        adapter = new PracticeAdapter();
        gvPracticeAnswer.setAdapter(adapter);


    }


    private void showPractice(int i) {
        tvPracticeTitle.setText("A" + String.valueOf(i + 1));
        mExpression = mPracticeList.get(i);
        tvPractice.setText(mExpression.getQuestion());
        setAnswerList();
    }

    private void setAnswerList() {
        mAnswerList[0] = mExpression.getResult();
        mAnswerList[1] = 3;
        mAnswerList[2] = 5;
        mAnswerList[3] = 13;
    }

    private void JudgeAnwer(Integer position) {
        System.out.println("---------->"+mAnswerList[position]);
        if(mAnswerList[position].equals(mExpression.getResult())){
            showPractice(position+1);
            adapter.notifyDataSetChanged();
        }else{
            Toast.makeText(context,"回答错误",Toast.LENGTH_SHORT).show();
        }
    }

    private class PracticeAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return mAnswerList.length;
        }

        @Override
        public Object getItem(int position) {
            return mAnswerList[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            Button btnAnswer;
            if(convertView == null){
                btnAnswer = new Button(context);
                btnAnswer.setLayoutParams(new GridView.LayoutParams(100, 100));
            }else{
                btnAnswer = (Button) convertView;
            }
            btnAnswer.setText(String.valueOf(mAnswerList[position]));
            btnAnswer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    JudgeAnwer(position);
                }
            });
            return btnAnswer;
        }
    }
}
